﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace MDIDemo.Windows.Report
{
    public partial class frmBangKeGTGT : DevExpress.XtraEditors.XtraForm
    {
        Dictionary<string, object> _valueList = new Dictionary<string, object>();

        public Dictionary<string, object> ValueList
        {
            get { return _valueList; }
            set { _valueList = value; }
        }
        DataSet _ds = null;
        public DataSet DataSource { get { return _ds; } }
        public string Type
        {
            get
            {
                return rType.EditValue.ToString();
            }

        }

        public frmBangKeGTGT()
        {
            InitializeComponent();
        }

        private void btnDong_Click(object sender, EventArgs e)
        {
            Close();
        }
        public DataSet BuildDataSourceNXHD()
        {
            string query = @"SELECT     BAOCAOTHUE.ID,BAOCAOTHUE.NGAYPS, BAOCAOTHUE.KYHIEUHD, BAOCAOTHUE.SOHD, BAOCAOTHUE.TENKH, BAOCAOTHUE.MSTHUE, BAOCAOTHUE.HANGHOA, 
                      SUM(BAOCAOTHUE.DOANHSO) AS DOANHSO, MAX(BAOCAOTHUE.THUESUAT) AS THUESUAT, SUM(BAOCAOTHUE.THUEGTGT) AS THUEGTGT, BAOCAOTHUE.DIACHI, 
                      BAOCAOTHUE.SOBT, BAOCAOTHUE.NGAYGD, BAOCAOTHUE.CUSINFO, MASOTHUE.TEN,BAOCAOTHUE.GHICHU
FROM         BAOCAOTHUE LEFT OUTER JOIN
                      MASOTHUE ON BAOCAOTHUE.MSTHUE = MASOTHUE.MST
WHERE     (MONTH(BAOCAOTHUE.NGAYPS) = {0}) AND (YEAR(BAOCAOTHUE.NGAYPS) = {1})  AND (BAOCAOTHUE.LOAIBT = '{2}' or BAOCAOTHUE.LOAIBT = '{3}')
GROUP BY BAOCAOTHUE.ID,BAOCAOTHUE.NGAYPS, BAOCAOTHUE.KYHIEUHD, BAOCAOTHUE.SOHD, BAOCAOTHUE.TENKH, BAOCAOTHUE.MSTHUE, BAOCAOTHUE.HANGHOA, 
                      BAOCAOTHUE.DIACHI, BAOCAOTHUE.SOBT, BAOCAOTHUE.NGAYGD, BAOCAOTHUE.CUSINFO, MASOTHUE.TEN,BAOCAOTHUE.GHICHU

SELECT     BAOCAOTHUE.ID, TONHANG.TENHANG, TONHANG.MAHANG, TONHANG.DVTINH, NXHOADON.SOLUONG, NXHOADON.GIABAN, NXHOADON.SOTIEN
FROM         BAOCAOTHUE LEFT OUTER JOIN
                      NXHOADON ON BAOCAOTHUE.ID = NXHOADON.NO LEFT OUTER JOIN
                      TONHANG ON NXHOADON.MAHANG = TONHANG.MAHANG
WHERE     (MONTH(BAOCAOTHUE.NGAYPS) = {0}) AND (YEAR(BAOCAOTHUE.NGAYPS) = {1}) AND (BAOCAOTHUE.LOAIBT = '{2}' or BAOCAOTHUE.LOAIBT = '{3}')";

            query = string.Format(query, txtMonth.EditValue, txtYear.EditValue, rType.EditValue, rType.EditValue.ToString() == "X-GTGT" ? "T-GTGT" : "C-GTGT");
            BUS.CommonControl ctr = new BUS.CommonControl();
            _ds = ctr.executeSelectQueries(query);
            return _ds;
        }
        private void btnPrint_Click(object sender, EventArgs e)
        {
            _ds = BuildDataSourceNXHD();

            if (_ds.Tables.Count == 0)
            {
                XtraMessageBox.Show("Dữ liệu không tồn tại!");
                return;
            }
            bool isdata = false;
            foreach (DataTable dt in _ds.Tables)
            {
                if (dt.Rows.Count > 0)
                {
                    isdata = true;
                    break;
                }
            }
            if (isdata == false)
            {
                XtraMessageBox.Show("Dữ liệu không tồn tại!");
                return;
            }
            _valueList.Add("Month", txtMonth.EditValue);
            _valueList.Add("Year", txtYear.EditValue);
            DialogResult = System.Windows.Forms.DialogResult.OK;
            Close();
        }

        private void txtMonth_Validating(object sender, CancelEventArgs e)
        {
            if (txtMonth.EditValue != null)
            {
                int num = Convert.ToInt32(txtMonth.EditValue);
                if (num < 1 || num > 12)
                    e.Cancel = true;
            }
            else e.Cancel = true;
        }

        private void txtYear_Validating(object sender, CancelEventArgs e)
        {
            if (txtYear.EditValue != null)
            {
                int num = Convert.ToInt32(txtYear.EditValue);
                if (num < 1900 || num > 9999)
                    e.Cancel = true;
            }
            else e.Cancel = true;
        }

        private void rType_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (rType.SelectedIndex == 0)
                lbTitle.Text = "Bảng kê hóa đơn, chứng từ hàng hóa, dịch vụ bán ra";
            else lbTitle.Text = "Bảng kê hóa đơn, chứng từ hàng hóa, dịch vụ mua vào";
        }

        private void frmBangKeGTGT_Load(object sender, EventArgs e)
        {
            txtMonth.EditValue = frmMain.__ngayMoSo.Month;
            txtYear.EditValue = frmMain.__ngayMoSo.Year;
        }
    }
}